﻿Imports System
Imports System.IO
Imports System.Data.OleDb
Imports System.Data
Imports System.Drawing
Imports System.Drawing.Imaging
Namespace Arwani
    Partial Class Backend_EditBrand
        Inherits System.Web.UI.Page

        Protected Sub cmdBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdBack.Click
            Response.Redirect("ManageBrands.aspx?divid=" & CInt(Request.QueryString("divid")))
        End Sub

        Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
            If Request.QueryString("bid") <> "0" Then
                Dim strQ As String
                Dim arwCon As New ArwaniController
                'Dim i As Integer = 0
                'strQ = "delete from division_brands where brandid=" & CInt(Request.QueryString("bid"))
                'arwCon.ExecuteNonQuery(strQ)
                'For i = 0 To chkDivisionList.Items.Count - 1
                '    If chkDivisionList.Items(i).Selected = True Then
                '        strQ = "insert into division_brands (divisionid,brandid) values (" & chkDivisionList.Items(i).Value & "," & CInt(Request.QueryString("bid")) & ")"
                '        arwCon.ExecuteNonQuery(strQ)
                '    End If
                'Next

                strQ = "update brand set brandname='" & Me.txtBrandName.Text.Replace("'", "''") & "',"
                strQ &= " branddesc='" & Me.txtBrandDesc.Text.Replace("'", "''") & "',"
                strQ &= " displayoption = " & Me.cboDisplayOption.SelectedItem.Value & ","
                strQ &= " sortorder = 0 "
                'strQ &= ",catid=" & Me.cboCategory.SelectedItem.Value
                strQ &= " where brandid=" & CInt(Request.QueryString("bid"))
      
                arwCon.ExecuteNonQuery(strQ)
                If Me.fileImage.PostedFile.FileName <> "" Then
                    Dim ext As String = IO.Path.GetExtension(Me.fileImage.PostedFile.FileName)
                    Dim strFileName As String = Guid.NewGuid.ToString() & ext
                    Me.fileImage.PostedFile.SaveAs(Server.MapPath("../Brands/" & strFileName))
                    Call ResizeImages(strFileName, "Thumb_" & strFileName, 178, 178)
                    Call ResizeImages(strFileName, "Thumb_small_" & strFileName, 180, 94)
                    strQ = "update brand set brandpic='" & strFileName & "' "
                    strQ &= " where brandid=" & CInt(Request.QueryString("bid"))
                    arwCon.ExecuteNonQuery(strQ)
                End If
                If Me.fileLogo.PostedFile.FileName <> "" Then
                    Dim ext As String = IO.Path.GetExtension(Me.fileLogo.PostedFile.FileName)
                    Dim strFileName As String = Guid.NewGuid.ToString() & ext
                    Me.fileLogo.PostedFile.SaveAs(Server.MapPath("../Brands/" & strFileName))
                    Call ResizeImages(strFileName, "Thumb_" & strFileName, 196, 67)
                    '   Call ResizeImages(strFileName, "Thumb_small_" & strFileName, 180, 94)
                    strQ = "update brand set brandlogo='" & strFileName & "' "
                    strQ &= " where brandid=" & CInt(Request.QueryString("bid"))
                    arwCon.ExecuteNonQuery(strQ)
                End If
            Else
                Dim strQ As String
                Dim arwCon As New ArwaniController
                Dim i As Integer = 0
                Dim brandID As Integer = 0
                strQ = "insert into brand (brandname,branddesc,displayoption,sortorder) values (" ',catid
                strQ &= "'" & Me.txtBrandName.Text.Replace("'", "''") & "',"
                strQ &= "'" & Me.txtBrandDesc.Text.Replace("'", "''") & "',"
                strQ &= " " & Me.cboDisplayOption.SelectedItem.Value & ",0)" '& Me.cboCategory.SelectedItem.Value & ""
                arwCon.ExecuteNonQuery(strQ)

                strQ = " SELECT LAST_INSERT_ID()"
                Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
                If dtTable.Rows.Count > 0 Then
                    brandID = CInt(dtTable.Rows(0)(0))
                End If

                'strQ = "delete from division_brands where brandid=" & CInt(brandID)
                'arwCon.ExecuteNonQuery(strQ)
                'For i = 0 To chkDivisionList.Items.Count - 1
                '    If chkDivisionList.Items(i).Selected = True Then
                '        strQ = "insert into division_brands (divisionid,brandid) values (" & chkDivisionList.Items(i).Value & "," & CInt(brandID) & ")"
                '        arwCon.ExecuteNonQuery(strQ)
                '    End If
                'Next

                'strQ = "update brand set brandname='" & Me.txtBrandName.Text.Replace("'", "''") & "',"
                'strQ &= " branddesc='" & Me.txtBrandDesc.Text.Replace("'", "''") & "',"
                'strQ &= " displayoption = " & Me.cboDisplayOption.SelectedItem.Value & ","
                'strQ &= " sortorder = " & Me.txtBrandSortOrder.Text
                'strQ &= " where brandid=" & CInt(brandID)
                'arwCon.ExecuteNonQuery(strQ)
                If Me.fileImage.PostedFile.FileName <> "" Then
                    Dim ext As String = IO.Path.GetExtension(Me.fileImage.PostedFile.FileName)
                    Dim strFileName As String = Guid.NewGuid.ToString() & ext
                    Me.fileImage.PostedFile.SaveAs(Server.MapPath("../Brands/" & strFileName))
                    Call ResizeImages(strFileName, "Thumb_" & strFileName, 178, 178)
                    Call ResizeImages(strFileName, "Thumb_small_" & strFileName, 180, 94)
                    strQ = "update brand set brandpic='" & strFileName & "' "
                    strQ &= " where brandid=" & CInt(brandID)
                    arwCon.ExecuteNonQuery(strQ)
                End If
                If Me.fileLogo.PostedFile.FileName <> "" Then
                    Dim ext As String = IO.Path.GetExtension(Me.fileLogo.PostedFile.FileName)
                    Dim strFileName As String = Guid.NewGuid.ToString() & ext
                    Me.fileLogo.PostedFile.SaveAs(Server.MapPath("../Brands/" & strFileName))
                    Call ResizeImages(strFileName, "Thumb_" & strFileName, 178, 178)
                    '   Call ResizeImages(strFileName, "Thumb_small_" & strFileName, 180, 94)
                    strQ = "update brand set brandlogo='" & strFileName & "' "
                    strQ &= " where brandid=" & CInt(brandID)
                    arwCon.ExecuteNonQuery(strQ)
                End If
            End If
            Response.Redirect("ManageBrands.aspx?divid=" & CInt(Request.QueryString("divid")))
        End Sub

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            If (Session("ADMIN_USER") IsNot Nothing) Then
                If Page.IsPostBack = True Then Exit Sub
                'Call loadDivision()

                'Call loadCategory(CInt(Request.QueryString("divid")))
                Call loadData()
            Else


                Response.Redirect("Default.aspx")
            End If
        End Sub

        'Public Sub loadCategory(ByVal divID As Integer)
        '    Dim arwCon As New ArwaniController
        '    Dim strQ As String = "select * from Category where divisionid=" & divID
        '    Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
        '    Me.cboCategory.DataTextField = "catname"
        '    Me.cboCategory.DataValueField = "catid"
        '    Me.cboCategory.DataSource = dtTable
        '    Me.cboCategory.DataBind()

        'End Sub
        'Public Sub loadDivision()
        '    Dim arwCon As New ArwaniController
        '    Dim strQ As String = "select * from division "
        '    Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)

        '    Me.chkDivisionList.DataSource = dtTable
        '    Me.chkDivisionList.DataTextField = "divisiontitle"
        '    Me.chkDivisionList.DataValueField = "divisionid"
        '    Me.chkDivisionList.DataBind()
        'End Sub
        Public Sub loadData()
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from brand where brandid=" & CInt(Request.QueryString("bid"))
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            Dim i As Integer = 0
            Dim r As Integer = 0
            If dtTable.Rows.Count > 0 Then
                If Not IsDBNull(dtTable.Rows(0)("brandname")) Then Me.txtBrandName.Text = dtTable.Rows(0)("brandname").ToString
                If Not IsDBNull(dtTable.Rows(0)("branddesc")) Then Me.txtBrandDesc.Text = dtTable.Rows(0)("branddesc").ToString
                '  If Not IsDBNull(dtTable.Rows(0)("sortorder")) Then Me.txtBrandSortOrder.Text = dtTable.Rows(0)("sortorder").ToString
                If Not IsDBNull(dtTable.Rows(0)("displayoption")) Then
                    If dtTable.Rows(0)("displayoption") <> 0 Then cboDisplayOption.Items.FindByValue(CInt(dtTable.Rows(0)("displayoption").ToString())).Selected = True
                End If
                'If Not IsDBNull(dtTable.Rows(0)("catid")) Then
                '    If dtTable.Rows(0)("catid") <> 0 Then cboCategory.Items.FindByValue(CInt(dtTable.Rows(0)("catid").ToString())).Selected = True
                'End If
                'strQ = "select * from division_brands  where brandid=" & CInt(Request.QueryString("bid"))
                'Dim dtDivTable As DataTable = arwCon.ExecuteQuery(strQ)
                'If dtDivTable.Rows.Count > 0 Then
                '    For r = 0 To dtDivTable.Rows.Count - 1
                '        For i = 0 To chkDivisionList.Items.Count - 1
                '            If CInt(chkDivisionList.Items(i).Value) = dtDivTable.Rows(r)("divisionid") Then
                '                chkDivisionList.Items(i).Selected = True
                '            End If
                '        Next
                '    Next
                'End If
            End If
        End Sub
        Sub ResizeImages(ByVal FileName As String, ByVal NewFileName As String, ByVal intWidth As Integer, ByVal intHeight As String)


            '            intWidth = 178 '*** Fix Width ***'  
            '           intHeight = 178   '*** If = 0 Auto Re-Cal Size ***'  

            Dim objGraphic As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("../Brands/" & FileName))

            Dim objBitmap As Bitmap
            '*** Calculate Height ***'  
            If intHeight > 0 Then
                objBitmap = New Bitmap(objGraphic, intWidth, intHeight)
            Else
                If objGraphic.Width > intWidth Then
                    Dim ratio As Double = objGraphic.Height / objGraphic.Width
                    intHeight = ratio * intWidth
                    objBitmap = New Bitmap(objGraphic, intWidth, intHeight)
                Else
                    objBitmap = New Bitmap(objGraphic)
                End If
            End If

            '*** Save As  ***'  
            objBitmap.Save(Server.MapPath("../Brands/" & NewFileName), objGraphic.RawFormat)

            '*** Close ***'  
            objGraphic.Dispose()

            '*** Nothing ***'  
            objBitmap = Nothing
            objGraphic = Nothing

        End Sub
    End Class
End Namespace